import java.awt.*;
import java.awt.event.*;
import java.awt.image.ImageObserver;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Enumeration;
import java.util.EnumSet;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.sound.midi.Sequence;
import javax.swing.*;
import javax.swing.event.*;



public class Pictocrypto extends Game implements ActionListener, KeyListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//Visual element variables
	private Image clueOne;
	private Image clueTwo;
	private Image clueThree;
	private Image clueFour;

	private JLabel wordHidden;
	private JLabel Score;
	private JLabel Strike;

	private JPanel HintOne;
	private JPanel HintTwo;
	private JPanel HintThree;
	private JPanel HintFour;
	private JPanel ScorePan;
	private JPanel StrikePan;




	//Functional element variables
	private boolean pressed=false;

	private EnumSet rando;

	private Iterator selectable;
	private String word;
	private String codedWord;
	private String[] completedWords;



	private HashMap<String, String> words;

	private int strike;
	private int completedSum = 0;
	private int score;
	private int potential;


	//game//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public Pictocrypto( String table, String level)
	{
		super(table,level);
	}

	public void init()
	{
		words = super.wordMap;
		completedWords = new String[words.size()];
		this.setLocationByPlatform(true);
		this.setSize(800, 600);
		this.setResizable(false);



		HintOne = new JPanel();
		HintTwo = new JPanel();
		//HintThree = new JPanel();
		//HintFour = new JPanel();

		ScorePan = new JPanel();
		StrikePan = new JPanel();

		HintOne.setBounds(0, 0, 600, 600);
		HintOne.setBackground(Color.red);
		HintTwo.setBounds(600, 200, 100,100);
		HintTwo.setBackground(Color.yellow);
		//HintThree.setBounds(0, 400, 200,200);
		//HintThree.setBackground(Color.magenta);
		//HintFour.setBounds(600, 400, 200, 200);
		//HintFour.setBackground(Color.blue);

		ScorePan.setBounds(600, 0, 100, 100);
		StrikePan.setBounds(600, 100, 100, 100);
		ScorePan.setBackground(Color.yellow);
		StrikePan.setBackground(Color.yellow);

		this.add(HintOne);
		this.add(HintTwo);
		//this.add(HintThree);
		//this.add(HintFour);

		this.add(ScorePan);
		this.add(StrikePan);



		clueOne = null;
		clueTwo = null;
		clueThree = null;
		clueFour = null;


		wordHidden = new JLabel();
		HintTwo.add(wordHidden);

		Strike = new JLabel();
		Score = new JLabel();
		StrikePan.add(Strike);
		ScorePan.add(Score);

		score = 0;
		strike = 0;

		Score.setText("Score: " + String.valueOf(score));
		Strike.setText("Strikes: "+ String.valueOf(strike));

		this.addKeyListener(this);



		this.startup();

		score = 0;
	}
	//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	public void startup(){
		//to implement initialize functions
		Random choose = new Random();
		strike = 0;
		potential = 1000;


		word =  words.values().toArray(new String[words.size()])[choose.nextInt(words.size())];
		int counter = 0;

		codedWord = "";
		counter = 0;
		while(counter<word.length()){
			codedWord = codedWord +"_";
			counter++;
		}
		//this.HintTwo.removeAll();
		this.wordHidden.setText(codedWord);
		//this.HintTwo.add(wordHidden);
		strike = 0;
		Strike.setText("Strikes: "+ String.valueOf(strike));


		//get image for words
		String location = "";
		Image hint = null;
		File imgFile = null;

		location = "imgs/"+word+".jpg";

		imgFile = new File(location);

		try{
			hint = ImageIO.read(imgFile);


		}catch (Exception e){
			try{
				hint = ImageIO.read(new File("imgs/wut.JPG"));
			}catch(Exception d){}//ummmmmmmmmmmmmmmmmm idk
		}

		this.HintOne.removeAll();

		this.HintOne.add(new JLabel(new ImageIcon(hint.getScaledInstance(600,600,Image.SCALE_DEFAULT))));
		/////////////////////



		this.setDefaultCloseOperation(this.DISPOSE_ON_CLOSE);
		this.setVisible(true);

	}

	@Override
	public void keyPressed(KeyEvent arg0) {
		// TODO Auto-generated method stub
		if(this.wordHidden.getText().contains(String.valueOf(arg0.getKeyChar()))){
			return;
		}
		if(pressed==false){
			pressed = true;
			if(arg0.getKeyChar() == '\n'){
				startup();
				return;


			}
			if(!codedWord.contains("_")){
				///////////

				startup();

				return;
			}
			int counter = 0;
			char[] replacer = new char [word.length()];
			replacer = codedWord.toCharArray();
			boolean change = false;
			while(counter < word.length()){

				if(word.charAt(counter)==arg0.getKeyChar()){
					//System.out.println(word.charAt(0));

					replacer[counter] = arg0.getKeyChar();
					change = true;
				}
				else if(word.charAt(counter) == Character.toUpperCase(arg0.getKeyChar())){ 
					replacer[counter] = Character.toUpperCase(arg0.getKeyChar());
					change = true;
				}
				counter++;

			}
			if(change == false){
				strike++;
				Strike.setText("Strikes: "+ String.valueOf(strike));
				if(strike >= 3){
					startup();
					return;
				}
			}
			else{
				score = score + 10;


			}
			counter =0;
			codedWord = "";
			while(counter < word.length()){
				codedWord = codedWord + String.valueOf(replacer[counter]);
				counter++;

			}



			this.wordHidden.setText(codedWord);
			if(!codedWord.contains("_")){
				score = score + (300/(1+strike));
			}
			Score.setText("Score: "+String.valueOf(score));
		}
	}

	@Override
	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub
		pressed = false;
	}

	@Override
	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub

	}

	/*public static void main(String args[])
	{
		Pictocrypto test = new Pictocrypto("animals", "1"); 
		test.startup();

	}*/
}
